<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <link href="style3.css" rel="stylesheet" type="text/css" />
        <title></title>
        <?php include 'jquery.php'; ?>
        <script type="text/javascript" src="functions.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                check_top();
            });
        </script>
    </head>
    <?php
    if (isset($_SESSION['lang']))
        require_once "./language_files/limitpapers_" . $_SESSION['lang'] . ".php";
    else
        require_once "./language_files/limitpapers_en.php";

    $id = $_SESSION['member_id']; //get the member id by a session variable and save it in a variable
    require_once "functions.php";
    if (isset($_GET['conference_id'])) { // checks if the conference id is passed from the previous page
        $confid = intval($_GET['conference_id']); //get the conference id that is passed from the previous page
    } else {
        $confid = intval($_POST['confID']);
    }
    $conference_name = get_conference_name($confid);
    echo"
	</br><span class=breadcrumbs><a href='subscriberConferencePage.php?conference_id={$confid}'>" . $conference_name . "</a>" . " > " . $lang['set'] . "</span></br></br>";

    $done = 0;

    $date = date('Y-m-d');
    $today = date("Y-m-d H:i:s", strtotime($date));
    $get_dates = mysql_query("SELECT rev_startDate FROM  conference c WHERE conference_id = '$confid'")
            or die(mysql_error());
    $row = mysql_fetch_assoc($get_dates);
    $start = $row['rev_startDate'];

    //get all categories in this conference
    $categories_in_conference = mysql_query("SELECT category, c.category_id FROM category c, conference_category cc WHERE c.category_id = cc.category_id AND cc.conference_id = $confid") or die(mysql_error());
    // save number of categories ,that is indicated by the number of rows in the result table, in a variable
    $numm = mysql_num_rows($categories_in_conference);

    // a loop that loops on the number of category rows in th resulted table to insert the category name and a drop down list that contains
    //values from 1 to 10 in html table
    echo "<table style='width:100%;'>"; // initiate the table
    echo "<tr><td>" . $lang['type'] . "</td><td>" . $lang['Slimit'] . "</td><td>" . $lang['limit'] . "</td></tr>";
    $h = true;
    if (isset($_POST['Save'])) { // check of the save button is clicked
        if (isset($_POST['Categories'])) { //check if a drop down list is selected
            for ($i = $numm; $i > 0; $i--) { // loop on the number of categories insert the value that is selected from each drop down list a the database
                if ($start > $today) {
                    $max = $_POST['Categories'][$i - 1]; // save the value of the selected item from the drop down list in a variable
                    // save the value of the hidden input which contain the category id of each drop down list to know which category are we inserting into
                    $cat_id = $_POST['c_id'][$i - 1];
                    $maximum_number = mysql_real_escape_string($max); // sanitizing the varible to insert it in the database
                    $category_id = mysql_real_escape_string($cat_id); // sanitizing the varible to insert it in the database
                    //if there is no value selected , view an alert
                    //insert the member id , conference id , category id and the maximumnumber of papers that the reviewer could accept in a conference in the database
                    $maximum_paper_insertions = mysql_query("UPDATE limit_paper SET maximum_papers = '$maximum_number'WHERE member_id = '$id' AND conference_id = '$confid' AND category_id = '$category_id'") or die(mysql_error());
                    //update the done variable as a counter to check later if all categories are inserted
                    $done++;
                }
            }
            //check if all the drop down lists are selected, an alert should appear to make sure they're all selected
            //check later if the couter of insertions is equal to the number of categories in the conference
        }
    }
    while ($row = mysql_fetch_assoc($categories_in_conference)) {
        $category_id = $row['category_id'];
        $check_to_insert = mysql_query("SELECT maximum_papers FROM limit_paper WHERE category_id = $category_id AND conference_id = $confid AND member_id = $id")
                or die(mysql_error());
        $num2 = mysql_num_rows($check_to_insert);

        if ($num2 == 0) {

            mysql_query("INSERT INTO limit_paper(member_id,conference_id,category_id,maximum_papers,assigned_papers) VALUES('$id', '$confid','$category_id','10','0')")
                    or die(mysql_error());
        }
        echo "<form method='post' action='' name='form'>"; //make a form with a post method
        echo "<input name='confID' type='hidden' value='{$confid}'/>";
        echo "<tr><td>", $row['category'], "</td>"; // print the name of each category
        // make a hidden input that takes a value the id of this category to keep track of the category id
        echo "<input name='c_id[]' type='hidden' value='{$row['category_id']}'/>";
        //echo "<td style='width:100px;'>";
        if ($start > $today) {
            print "<td><select name=\"Categories[]\" id=\"{$row['category_id']}\" \">"; // create a drop down list to each category
            $h = true;
        } else {
            print "<td><select name=\"Categories[]\" id=\"{$row['category_id']}\" disabled=\"disabled\" \">";
            $h = false;
        }
        $maxP = mysql_query("SELECT maximum_papers FROM limit_paper WHERE category_id = $category_id AND conference_id = $confid AND member_id = $id") or die(mysql_error());
        $limit = 10;
        while ($row2 = mysql_fetch_assoc($maxP)) {
            $limit = $row2['maximum_papers'];
        }

        //Populate it with values from 1 to 10
        echo '<option value="' . $limit . '">' . $lang['select_num'] . '</option>';
        echo '<option value="' . 0 . '">' . 0 . '</option>';
        echo '<option value="' . 1 . '">' . 1 . '</option>';
        echo '<option value="' . 2 . '">' . 2 . '</option>';
        echo '<option value="' . 3 . '">' . 3 . '</option>';
        echo '<option value="' . 4 . '">' . 4 . '</option>';
        echo '<option value="' . 5 . '">' . 5 . '</option>';
        echo '<option value="' . 6 . '">' . 6 . '</option>';
        echo '<option value="' . 7 . '">' . 7 . '</option>';
        echo '<option value="' . 8 . '">' . 8 . '</option>';
        echo '<option value="' . 9 . '">' . 9 . '</option>';
        echo '<option value="' . 10 . '">' . 10 . '</option>';
        echo "<td>", $limit, "</td>";
    }
    if ($h == false) {
        echo "<script type='text/javascript'>alert('You cannot update papers limit after papers submission due date which was " . $start . "');</script>";
    }

    echo "</select></td></tr>"; // close drop down list
    echo "<td colspan='2'><p align='center'><input type ='submit' name ='Save' value ='{$lang['save']}' /></p></td>";
    echo "</table>"; // close table
    echo "</form>";
    if ($done == $numm) {
        echo "<script type='text/javascript'>alert('done');</script>";
    }
    ?>
</html>

